package marseillaisdu90.student_life;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class StudentLife extends Activity {

	private MonApplication appli;
	private boolean menuLance;
	private TextView moyenne;
	private TextView vie;
	private TextView piece;
	private final int CODE_ACTIVITE_MENU = 1;
	private FenetreJeuView fjv;
	private boolean menuTermine;
	private Button joystickH;
	private Button joystickD;
	private Button joystickG;
	private Button joystickB;
	private Button boutonAction;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        appli = (MonApplication)getApplication();
        appli.setSl(this);
        
        setContentView(R.layout.main);

        menuLance = false;
        menuTermine = false;
        
        moyenne = (TextView)findViewById(R.id.moyenne);
        vie = (TextView)findViewById(R.id.vie);
        piece = (TextView)findViewById(R.id.piece);
        fjv = (FenetreJeuView)findViewById(R.id.StudentLifeView);
        
        
    	joystickH = (Button)findViewById(R.id.imageButton1);
    	joystickH.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				joystickH.getBackground().setAlpha(255);
				joystickH.invalidate();
				return false;
			}
		});
    	joystickH.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				joystickH.getBackground().setAlpha(100);
				joystickH.invalidate();
				fjv.deplacement(0, -1);
			}
		});
    	
    	
    	joystickG = (Button)findViewById(R.id.imageButton2);
    	joystickG.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				joystickG.getBackground().setAlpha(255);
				joystickG.invalidate();
				return false;
			}
		});
    	joystickG.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				joystickG.getBackground().setAlpha(100);
				joystickG.invalidate();
				fjv.deplacement(-1, 0);
			}
		});
    	
    	
    	joystickD = (Button)findViewById(R.id.imageButton21);
    	joystickD.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				joystickD.getBackground().setAlpha(255);
				joystickD.invalidate();
				return false;
			}
		});
    	joystickD.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				joystickD.getBackground().setAlpha(100);
				joystickD.invalidate();
				fjv.deplacement(1, 0);
			}
		});
    	
    	
    	joystickB = (Button)findViewById(R.id.imageButton3);
    	joystickB.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				joystickB.getBackground().setAlpha(255);
				joystickB.invalidate();
				return false;
			}
		});
    	joystickB.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				joystickB.getBackground().setAlpha(100);
				joystickB.invalidate();
				fjv.deplacement(0, 1);
			}
		});
    	
    	
    	boutonAction = (Button)findViewById(R.id.bouton_action);
    	boutonAction.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				boutonAction.getBackground().setAlpha(255);
				//boutonAction.destroyDrawingCache();
				//boutonAction.buildDrawingCache();
				//boutonAction.forceLayout();
				//boutonAction.refreshDrawableState();
				//boutonAction.postInvalidate();
				boutonAction.invalidate();
				return false;
			}
		});
    	boutonAction.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boutonAction.getBackground().setAlpha(100);
				boutonAction.invalidate();
				fjv.action();
			}
		});
    	
    	joystickB.getBackground().setAlpha(100);
    	joystickD.getBackground().setAlpha(100);
    	joystickG.getBackground().setAlpha(100);
    	joystickH.getBackground().setAlpha(100); 
    	boutonAction.getBackground().setAlpha(100);
        
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Log.i("oncreate : config", "densite : " + metrics.density + ", densite dpi : " + metrics.densityDpi + ", h pixel : " + metrics.heightPixels + ", w pixel : " + metrics.widthPixels + ", xdpi : " + metrics.xdpi);
        
        mettreAJourMenu();
        
        /*
        if(appli.getMp() == null) {
        	appli.setMp(MediaPlayer.create(this, R.raw.out_of_this_club));
	        
	        try {
				mp.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	        //mp.reset();
        	appli.getMp().start();
	        //mp.pause();
        	appli.getMp().setLooping(true);
        	appli.getMp().setOnCompletionListener(new OnCompletionListener() {
	            @Override
	            public void onCompletion(MediaPlayer mp) {
	                //TODO
	            	Log.i("music","musique fini");
	            	mp.release();
	            }
	        });
        }
    	*/
        
        //vue.setTimerView(timerView);
    }

	@Override
	public void onBackPressed() {
		appli.fermerMusique();
		super.onBackPressed();
	}
	
	@Override
	protected void onDestroy() {
		Log.i("onDestroy","onDestroy");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.i("onPause","onPause");
		Log.i("menuLance on pause", ""+menuLance);
		if(!menuLance)
			appli.pauseMusique();
		super.onPause();
	}

	@Override
	protected void onResume() {
		Log.i("onResume","onResume");
		super.onResume();
	}
	
	@Override
	protected void onStop() {
		Log.i("onStop","onStop");
		super.onStop();
	}
	
	@Override
	protected void onRestart() {
		//appli.demarrerMusique();
		Log.i("onRestart","onRestart");
		super.onRestart();
	}
	
	@Override
	protected void onStart() {
		appli.demarrerMusique();
		Log.i("onStart","onStart");
		super.onRestart();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		Log.i("onNewIntent","onNewIntent");
		super.onNewIntent(intent);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.i("onRestoreInstanceState","onRestoreInstanceState");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.i("onSaveInstanceState","onSaveInstanceState");
		super.onSaveInstanceState(outState);
	}
	
	public void mettreAJourMenu() {
		
		piece.setText(appli.modele.ListePiece.id.get(appli.modele.nopiece_courante).getNomPiece());
		moyenne.setText(appli.modele.joueur.getMoyenne()+"/20");
		vie.setText("Vie : "+appli.modele.joueur.getVie());
		
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.i("onConfigurationChanged","onConfigurationChanged");
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
			case CODE_ACTIVITE_MENU :
				menuTermine = true;
				Log.i("menuLance result", ""+menuLance);
				fjv.getmThread().MAJIG();
				break;
			default : 
				break;
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		
		if(menuTermine) {
			menuLance = false;
			menuTermine = false;
		}
		else if (keyCode == KeyEvent.KEYCODE_MENU && !menuLance) {
			menuLance = true;
			Log.i("menuLance key code", ""+menuLance);
			Intent intent = new Intent(this, Menu_Inventaire.class);
			//Bundle objetbunble = new Bundle();
			//objetbunble.putLong("id", identifiantTache);
			//intent.putExtras(objetbunble);
			startActivityForResult(intent, CODE_ACTIVITE_MENU);
			//transitionActivity();
			this.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		}
		
		if(keyCode == KeyEvent.KEYCODE_BACK && !menuLance)
			finish();
		
		return true;
	}
	
	
	

}